//
//  UFUserDefault.m
//  universityFace
//
//  Created by 阮 沧晖 on 14-9-25.
//  Copyright (c) 2014年 阮 沧晖. All rights reserved.
//

#import "DDSharedUser.h"


static DDSharedUser *ufUserDefault;
@implementation DDSharedUser

+ (DDSharedUser *)shared
{
    if (ufUserDefault == nil){
        @synchronized(self){
            ufUserDefault = [[DDSharedUser alloc]init];
        }
    }
    return ufUserDefault;
}

- (id)init
{
    self = [super init];
    if (self) {
        userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
}

- (void)updateUserDefaults:(id)value forKey:(NSString *)key
{
    [userDefaults setObject:value forKey:key];
}

- (void)updateUserDefaultsIntValue:(NSInteger)value forKey:(NSString *)key
{
    [userDefaults setInteger:value forKey:key];
}

- (void)updateUserDefaultsBoolValue:(BOOL)value forKey:(NSString *)key
{
    [userDefaults setBool:value forKey:key];
}

- (NSInteger)intValueForKey:(NSString *)key
{
    return [userDefaults integerForKey:key];
}

- (id)userDefaultValueForKey:(NSString *)key
{
    return [userDefaults objectForKey:key];
}

- (void)updateLoginInfosForUsername:(NSString *)usernme forPassword:(NSString *)password  forPhonecode:(NSString *)phonecode
{
    [ufUserDefault updateUserDefaults:usernme forKey:kUsername];
    [ufUserDefault updateUserDefaults:password forKey:kPassword];
    [ufUserDefault updateUserDefaults:phonecode forKey:k_phoneCode];
}

- (void)resetLoginInfos{
    
    [ufUserDefault updateUserDefaults:@"" forKey:kUsername];
    [ufUserDefault updateUserDefaults:@"" forKey:kPassword];
}

- (void)saveExchangeInfo:(DDMGiftOrderCell *)cell{
    NSDictionary *exchangeInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                  cell.consignee, @"consignee",
                                  cell.areaName, @"district",
                                  cell.address, @"address",
                                  (cell.zipCode == nil?@"" : cell.zipCode), @"postCode",
                                  cell.phone, @"phoneNumber",
                                  nil];
    [self updateUserDefaults:exchangeInfo forKey:@"KExchangeInfo"];
}

- (DDAddressCell *)getExchangeInfo
{
    NSDictionary *exchangeInfo = [self userDefaultValueForKey:@"KExchangeInfo"];
    DDAddressCell *cell = [[DDAddressCell alloc] init];
    cell.consignee = [exchangeInfo objectForKey:@"consignee"];
    cell.district = [exchangeInfo objectForKey:@"district"];
    cell.address = [exchangeInfo objectForKey:@"address"];
    cell.postCode = [exchangeInfo objectForKey:@"postCode"];
    cell.phoneNumber = [exchangeInfo objectForKey:@"phoneNumber"];
    return cell;
}

@end
